﻿using UnityEngine;

/// <summary>
/// 滞空
/// </summary>
public class FSMStateAirborne : FSMStateBase
{
    protected override string getName => "airborne";

    private float acceleration { get; } = UnitDefine.BASE_ADJUST_ACCELERATION;

    protected override void OnExecute()
    {
        var axis = fsm.parameters.GetParam<float>("horizontalAxis");
        LookAt(axis);
        ModifyVelocity(axis);
    }

    private void LookAt(float axis)
    {
        if (axis != 0)
            fsm.entity.animator.direction = new Vector3(axis, 1, 1);
    }

    private void ModifyVelocity(float axis)
    {
        float currentVx = fsm.entity.physics.velocity.x;
        float deltaVx = axis * Time.deltaTime * currentVx * acceleration;
        if (currentVx > 0)
        {
            currentVx = Mathf.Clamp(currentVx + deltaVx, 0, currentVx);
        }
        else
        {
            currentVx = Mathf.Clamp(currentVx - deltaVx, currentVx, 0);
        }
        fsm.entity.physics.velocity = new Vector2(currentVx, fsm.entity.physics.velocity.y);
    }
}